﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RemoveOutermostParentheses
{
    public class Solution
    {
        public string RemoveOuterParentheses(string S)
        {
            var sb = new StringBuilder();
            var stack = new Stack<char>();

            foreach (var c in S)
            {
                if (c == '(')
                {
                    stack.Push(c);
                }

                if (stack.Count > 1)
                {
                    sb.Append(c);
                }

                if (c == ')') 
                {
                    stack.Pop();
                }
            }
            return sb.ToString();
        }
    }
}
